import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QGridLayout()
        
        groupList=QGroupBox('ListView')
        groupListLayout=QVBoxLayout()        
        toolBar=QToolBar()

        btn=QPushButton('Add')
        btn.clicked.connect(self.ListAdd)
        toolBar.addWidget(btn)

        btn=QPushButton('Insert')
        btn.clicked.connect(self.ListInsert)
        toolBar.addWidget(btn)

        btn=QPushButton('Delete')
        btn.clicked.connect(self.ListDelete)
        toolBar.addWidget(btn)

        groupListLayout.addWidget(toolBar)
        self.listView=QListView() 
        self.listModel=QStandardItemModel()          
        self.listView.setModel(self.listModel)       
        
        groupListLayout.addWidget(self.listView)
        groupList.setLayout(groupListLayout)
        layout.addWidget(groupList,0,0)

        groupTree=QGroupBox('TreeView')
        groupTreeLayout=QVBoxLayout()        
        toolBar=QToolBar()

        btn=QPushButton('Add')
        btn.clicked.connect(self.TreeAdd)
        toolBar.addWidget(btn)

        btn=QPushButton('AddChild')
        btn.clicked.connect(self.TreeAddChild)
        toolBar.addWidget(btn)

        btn=QPushButton('Insert')
        btn.clicked.connect(self.TreeInsert)
        toolBar.addWidget(btn)

        btn=QPushButton('InsertChild')
        btn.clicked.connect(self.TreeInsertChild)
        toolBar.addWidget(btn)

        btn=QPushButton('Delete')
        btn.clicked.connect(self.TreeDelete)
        toolBar.addWidget(btn)

        groupTreeLayout.addWidget(toolBar)
        self.treeView=QTreeView()
        self.treeModel=QStandardItemModel()
        self.treeModel.setHorizontalHeaderLabels(['Id','Text'])
        self.treeView.setModel(self.treeModel)
        groupTreeLayout.addWidget(self.treeView)
        groupTree.setLayout(groupTreeLayout)
        layout.addWidget(groupTree,0,1)

        groupTable=QGroupBox('TableView')
        groupTableLayout=QVBoxLayout()        
        toolBar=QToolBar()

        btn=QPushButton('Add')
        btn.clicked.connect(self.TableAdd)
        toolBar.addWidget(btn)        

        btn=QPushButton('Insert')
        btn.clicked.connect(self.TableInsert)
        toolBar.addWidget(btn)

        btn=QPushButton('Delete')
        btn.clicked.connect(self.TableDelete)
        toolBar.addWidget(btn)

        groupTableLayout.addWidget(toolBar)
        self.tableView=QTableView()
        self.tableView.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)#只读
        self.tableView.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)#行选择
        self.tableView.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)#单行
        self.tableModel=QStandardItemModel()
        self.tableModel.setHorizontalHeaderLabels(['Id','Name'])
        self.tableView.setModel(self.tableModel)
        groupTableLayout.addWidget(self.tableView)
        groupTable.setLayout(groupTableLayout)
        layout.addWidget(groupTable,1,0)

        groupColumn=QGroupBox('ColumnView')
        groupColumnLayout=QVBoxLayout()        
        toolBar=QToolBar()

        btn=QPushButton('Add')
        btn.clicked.connect(self.ColumnAdd)
        toolBar.addWidget(btn)

        btn=QPushButton('AddChild')
        btn.clicked.connect(self.ColumnAddChild)
        toolBar.addWidget(btn)

        btn=QPushButton('Insert')
        btn.clicked.connect(self.ColumnInsert)
        toolBar.addWidget(btn)

        btn=QPushButton('InsertChild')
        btn.clicked.connect(self.ColumnInsertChild)
        toolBar.addWidget(btn)

        btn=QPushButton('Delete')
        btn.clicked.connect(self.ColumnDelete)
        toolBar.addWidget(btn)

        groupColumnLayout.addWidget(toolBar)
        self.columnView=QColumnView()
        self.columnModel=QStandardItemModel()
        self.columnView.setModel(self.columnModel)

        groupColumnLayout.addWidget(self.columnView)
        groupColumn.setLayout(groupColumnLayout)
        layout.addWidget(groupColumn,1,1)

        self.setLayout(layout)
    def ListAdd(self):
        text = f'Item{self.listModel.rowCount()}'        
        self.listModel.appendRow(QStandardItem(text))        
        self.parent.OnShowMessage(f"ListView Add item '{text}'")

    def ListInsert(self):
        index=self.listView.currentIndex()
        if index.isValid()==False:return
        text = f'Item{self.listModel.rowCount()}' 
        self.listModel.insertRow(index.row(),QStandardItem(text))
        self.parent.OnShowMessage(f"ListView Insert item '{text}' at {index.row()}")

    def ListDelete(self):
        index=self.listView.currentIndex()
        if index.isValid()==False:return
        text=self.listModel.data(index)
        self.listModel.removeRow(index.row())
        self.parent.OnShowMessage(f"ListView Delete item '{text}'")

    def TreeAdd(self):        
        row=self.treeModel.rowCount()
        text = f'Node{row}'
        self.treeModel.appendRow([QStandardItem(str(row)),QStandardItem(text)])
        self.parent.OnShowMessage(f"TreeView Add item '{text}'")
    def TreeAddChild(self):        
        index = self.treeView.currentIndex()        
        if index.isValid()==False:return
        itemId = self.treeModel.item(index.row(),0)
        itemText = self.treeModel.item(index.row(),1)
        row = itemId.rowCount()
        id=f'{itemId.text()}-{row}'
        text = f'{itemText.text()}-Child{row}'
        itemId.appendRow([QStandardItem(id),QStandardItem(text)])        
              
        self.treeView.expand(index)#展开
        self.parent.OnShowMessage(f"TreeView Add child '{text}'")
    def TreeInsert(self):
        index = self.treeView.currentIndex()        
        if index.isValid()==False:return
        row=self.treeModel.rowCount()
        text = f'Node{row}'
        self.treeModel.insertRow(index.row(),[QStandardItem(str(row)),QStandardItem(text)])
        self.parent.OnShowMessage(f"TreeView Insert '{text}' at {index.row()}")

    def TreeInsertChild(self):
        index = self.treeView.currentIndex()        
        if index.isValid()==False:return
        currentItem=self.treeModel.itemFromIndex(index)        
        if currentItem.parent() is None:return    
        parentItem=currentItem.parent() 
        parentId = self.treeModel.item(parentItem.row(),0)
        parentText=self.treeModel.item(parentItem.row(),1)   
        row = parentItem.rowCount()
        id=f'{parentId.text()}-{row}'
        text = f'{parentText.text()}-Child{row}'
        parentItem.insertRow(currentItem.row(),[QStandardItem(id),QStandardItem(text)])
        self.parent.OnShowMessage(f"TreeView Insert child '{text}' at {currentItem.row()}")

    def TreeDelete(self):
        index = self.treeView.currentIndex()        
        currentItem = self.treeModel.itemFromIndex(index)
        text = currentItem.text()
        if currentItem.parent() is None:
            self.treeModel.removeRow(index.row())  
        else:
            currentItem.parent().removeRow(index.row())
        self.parent.OnShowMessage(f"TreeView Delete item '{text}'")

    def TableAdd(self):
        row=self.tableModel.rowCount()
        text = f'Name{row}'
        self.tableModel.appendRow([QStandardItem(str(row)),QStandardItem(text)])
        self.parent.OnShowMessage(f"TableView Add item '{text}'")    
    
    def TableInsert(self):
        index=self.tableView.currentIndex()
        if index.isValid()==False:return
        row=self.tableModel.rowCount()
        text = f'Name{row}'
        self.tableModel.insertRow(index.row(),[QStandardItem(str(row)),QStandardItem(text)])
        self.parent.OnShowMessage(f"TableView Insert item '{text}' at {index.row()}")    
        
    def TableDelete(self):
        index = self.tableView.currentIndex()        
        itemId=self.tableModel.item(index.row(),0)
        itemText=self.tableModel.item(index.row(),1)
        self.parent.OnShowMessage(f"TableView Delete item '{itemText.text()}'")
        self.tableModel.removeRow(index.row())        

    def ColumnAdd(self):
        row=self.columnModel.rowCount()
        text = f'Column{row}'
        self.columnModel.appendRow(QStandardItem(text))
        self.parent.OnShowMessage(f"ColumnView Add item '{text}'")
    
    def ColumnAddChild(self):
        index=self.columnView.currentIndex()
        if index.isValid()==False:return
        currentItem=self.columnModel.itemFromIndex(index)
        row=currentItem.rowCount()
        text = f'{currentItem.text()}-Column{row}'
        currentItem.appendRow(QStandardItem(text))
        self.parent.OnShowMessage(f"ColumnView Add child '{text}'")
    
    def ColumnInsert(self):
        index=self.columnView.currentIndex()
        if index.isValid()==False:return
        currentItem=self.columnModel.itemFromIndex(index)
        if currentItem.parent() is None:
            text = f'Column{self.columnModel.rowCount()}'
            self.columnModel.insertRow(index.row(),QStandardItem(text))
        else:
            parentItem=currentItem.parent()
            text = f'{parentItem.text()}-Column{parentItem.rowCount()}'
            parentItem.insertRow(currentItem.row(),QStandardItem(text))
        self.parent.OnShowMessage(f"ColumnView Insert item '{text}'")

    def ColumnInsertChild(self):
        index=self.columnView.currentIndex()
        if index.isValid()==False:return
        currentItem=self.columnModel.itemFromIndex(index)
        if currentItem.parent() is None:
            text = f'Column{self.columnModel.rowCount()}'
            self.columnModel.insertRow(index.row(),QStandardItem(text))
        else:
            parentItem=currentItem.parent()
            text = f'{parentItem.text()}-Column{parentItem.rowCount()}'
            parentItem.insertRow(currentItem.row(),QStandardItem(text))
        self.parent.OnShowMessage(f"ColumnView Insert child '{text}'")
        
    def ColumnDelete(self):
        index=self.columnView.currentIndex()
        if index.isValid()==False:return
        currentItem=self.columnModel.itemFromIndex(index)
        self.parent.OnShowMessage(f"ColumnView delete item '{currentItem.text()}'")
        if currentItem.parent is None:
            self.columnModel.removeRow(index.row())
        else:
            currentItem.parent().removeRow(index.row())
        
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet